﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using TracyGUI.View;
using SharedLib.WinFormMVCFramework;
using TracyGUI.Model;
using Tracy;
using SharedLib;
using System.IO;
using TracyGUI.Logging;

namespace TracyGUI.Controller
{
    class NotifyIconViewController : ControllerBase<INotifyIconView>, IController<INotifyIconView>
    {
        protected override INotifyIconView ResolveView()
        {
            return Tracy.TracyFacade.Instance.DIContainer.Resolve<INotifyIconView>();
        }

        public NotifyIconViewController()
        {
            View.BallonTipClicked += new EventHandler(View_BallonTipClicked);
            View.TracyPreferencesClicked += new EventHandler(View_TracyPreferencesClicked);
            NotificationManager.NotificationArrived += new EventHandler<NotificationEventArgs>(NotificationManager_NotificationArrived);
            UITraceListener.LogTraced += new EventHandler<GenericEventArgs<string>>(UITraceListener_LogTraced);
            TracyFacade.Instance.MessageDispatcher.OnException += new EventHandler<ExceptionEventArgs>(MessageDispatcher_OnException);
        }

        void View_TracyPreferencesClicked(object sender, EventArgs e)
        {
            PreferencesViewController preferencesVC = new PreferencesViewController();
            preferencesVC.View.ShowModal();
        }

        void UITraceListener_LogTraced(object sender, GenericEventArgs<string> e)
        {
            StringReader sr = new StringReader(e.Data);
            View.DisplayException(sr.ReadLine(), e.Data);
        }

        void NotificationManager_NotificationArrived(object sender, NotificationEventArgs e)
        {
            View.DisplayMessage(e.Title, e.Text, e.Icon);
        }

        void View_BallonTipClicked(object sender, EventArgs e)
        {
            Tracy.TracyFacade.Instance.ExceptionManager.Process(() =>
            {
                NotificationManager.PerformLastCallback();
            }, "UI Policy");
        }

        void MessageDispatcher_OnException(object sender, ExceptionEventArgs e)
        {
            TracyFacade.Instance.ExceptionManager.HandleException(e.EmbededException, "UI Policy");
        }

    }
}
